//
//  FlipsideViewController.m
//  FortuneCookieIOS4
//
//  Created by Olaf on 26.06.10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "FlipsideViewController.h"


@implementation FlipsideViewController

@synthesize delegate;
@synthesize about;
@synthesize submitReview, askAQuestion;
#pragma mark -
-(void) setupWebView { 
	NSError *error = nil;
	[[self about] setBackgroundColor:[UIColor colorWithRed:0.094 green:0.094 blue:0.106 alpha:1]];
	NSString * styleFileToUse = [NSString stringWithString:@"AboutStyle.txt"]; 
	NSString * htmlSourceFile = [NSString stringWithString:@"AboutText.txt"];
	FuelLogAppDelegate *appDelegate = (FuelLogAppDelegate *)[[UIApplication sharedApplication] delegate];

	NSString * htmlSourceFileToUse =  [[appDelegate countryPrefix] stringByAppendingString:htmlSourceFile];
	NSString *appDir = [[NSBundle mainBundle] resourcePath];
	NSString *htmlPath = [appDir stringByAppendingPathComponent:htmlSourceFileToUse];
	NSString *stylePath = [appDir stringByAppendingPathComponent:styleFileToUse];
	debug(@"htmlPath %@",htmlPath);
	debug(@"stylePath %@",stylePath);
	
	NSString * htmlHeader = [NSString stringWithContentsOfFile:stylePath encoding:NSUTF8StringEncoding error:&error ];
	NSString * htmlBody = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:&error ];
	CFStringRef currentVersionString;
    currentVersionString = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey);
	NSString * html = [NSString stringWithFormat:@"%@ Version: %@, %@",htmlHeader, currentVersionString, kBuildNumber];
	html = [html stringByAppendingString:htmlBody];
	[[self about] loadHTMLString:html baseURL:nil ];	
}

-(void)webViewDidFinishLoad: (UIWebView *)pageView {
	if (firstLoad) {
		firstLoad = NO;
		[UIView beginAnimations:@"web" context:nil];
		self.about.alpha = 1.0;
		[UIView commitAnimations];
    }
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
	NSURL *url = [request URL];
	if (!firstLoad && ([[url scheme] isEqual:@"http"])) {
		[[UIApplication sharedApplication] openURL:url];
		return NO;
	}
	return YES;
}

#pragma mark -

- (void)viewDidLoad { 
	[super viewDidLoad];
	firstLoad = YES;
	self.about.delegate =self;
	self.about.alpha = 0.0;
 
 
    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];   
	[self setupWebView] ; 
	[self.submitReview useSystemTouchStyle];  
 	[self.askAQuestion useSystemTouchStyle];
  }

-(void)viewWillAppear:(BOOL)animated {
}

- (IBAction)done:(id)sender {
	[self.delegate flipsideViewControllerDidFinish:self];	
}

-(void)sendMail:(NSString *) subjectString messageBodyString:(NSString *) messageBodyString  {
	MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
	mail.mailComposeDelegate = self;
	if ([MFMailComposeViewController canSendMail]) {
		//Setting up the Subject, recipients, and message body.
		[mail setToRecipients:[NSArray arrayWithObjects:@"iphone@greck.eu",nil]];
		[mail setSubject:subjectString];
		[mail setMessageBody:messageBodyString isHTML:NO];
		//Present the mail view controller
		[self presentModalViewController:mail animated:YES];
	}
	//release the mail
	[mail release];
}	

- (IBAction)doReview:(id)sender{
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.com/app/fuellogevo"]];
}

 

- (IBAction)askAQuestion:(id)sender{
	
	MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
	mail.mailComposeDelegate = self;
	NSString * subjectString = [NSString stringWithString:(NSLocalizedString(@"FlibSide_askAQuestion Subject",@"Feedback"))];
	NSString * messageBodyString = [NSString stringWithString:(NSLocalizedString(@"FlibSide_askAQuestion msgBody",@"Hello Olaf, \nMy Name is:"))];
	[self sendMail:subjectString messageBodyString:messageBodyString]; 	
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
	
	[self dismissModalViewControllerAnimated:YES];
	if (result == MFMailComposeResultFailed) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:(NSLocalizedString(@"FlibSide_askAQuestion fail",@"Message Failed!" ))
														message:(NSLocalizedString(@"FlibSide_askAQuestion fail msg",@"Your email has failed to send" ))
													   delegate:self 
											  cancelButtonTitle:(NSLocalizedString(@"FlibSide_askAQuestion Dismiss",@"Dismiss" ))
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (void)dealloc {
//	[appDelegate release];
	about.delegate = nil;
    [about release]; 
	[submitReview release];
	[askAQuestion release];
	[super dealloc];
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
	
#pragma mark FIXME crach at startup with complaints about viewContoller
}
- (id)valueForUndefinedKey:(NSString *)key {
	NSLog(@"this class is not key value coding-compliant for the key %@",key);
	return nil;
}

@end
